package WebTools;

use warnings;
use base qw(Exporter);
use strict;
use Carp;
use lib ("d:/test_project/lib");
use Error qw(:try);
# print "1";
# use Backbone::Exception;
# use Backbone::Exception::ScriptError;

# our @EXPORT = qw(
	# javascript
	# set_location
	# refresh_directory_frame 
# );


# sub get_newline2br_filter {
	# return sub {
		# my $s = shift;
		# $s =~ s/\r\n|\r|\n/<br \/>/g;
		# return $s;
	# };
# }


# sub get_browser_info {
	
	# my $single_value = shift;
	
	# my $ua = lc $ENV{'HTTP_USER_AGENT'};
	# my $check = sub {
		# return $ua =~ /$_[0]/;
	# };
	
	# my %info;
	
	# $info{isOpera} = $check->( 'opera' );
	# $info{isChrome} = $check->( qr/\bchrome\b/ );
	# $info{isWebKit} = $check->( 'webkit' );

	# $info{isSafari} = $check->( 'safari' ) && !$info{isOpera};
	# $info{isSafari2} = $info{isSafari} && $check->( 'applewebkit/4' );
	# $info{isSafari3} = $info{isSafari} && $check->( 'version/3' );
	# $info{isSafari4} = $info{isSafari} && $check->( 'version/4' );
	
	# $info{isIE} = !$info{isOpera} && $check->( 'msie' );
	# $info{isIE7} = $info{isIE} && $check->( 'msie 7' );
	# $info{isIE8} = $info{isIE} && !$info{isIE7} && $check->( 'msie 8' );
	# $info{isIE9} = $info{isIE} && $check->( 'msie 9' );
	# $info{isIE6} = $info{isIE} && !$info{isIE7} && !$info{isIE8} && !$info{isIE9};
	
	# $info{isGecko} = !$info{isWebKit} && $check->( 'gecko' );
	# $info{isGecko2} = $info{isGecko} && $check->( 'rv:1.8' );
	# $info{isGecko3} = $info{isGecko} && $check->( 'rv:1.9' );
	
	# $info{isWindows} = $check->( qr/windows|win32/ );
	# $info{isMac} = $check->( qr/macintosh|mac os x/ );
	# $info{isAir} = $check->( 'adobeair' );
	# $info{isLinux} = $check->( 'linux' );

	# if(defined $single_value){
		# if(exists $info{$single_value}){
			# return $info{$single_value};
		# } else {
			# throw Backbone::Exception::ScriptError(message=>"Wrong argument '$single_value'");
		# }
	# } else {
		# return \%info;
	# }
# }

# sub reload_opener {
	# return qq(<html><body onload='window.opener.location.reload();window.close()'/></html>);
# }

# sub relocate_opener {
	# my ($new_location) = @_;
	# return qq(<html><body onload='window.opener.location="$new_location";window.close()'/></html>);
# }

# #
# # removes plings from the cgi->param values
# #
# sub discardLight {
	# my($cgi) = shift @_;		
	# foreach my $key ($cgi->param()) {
		# my $value = $cgi->param($key);
		# next if(!defined($value));
		# $value=~ s/'//g;	
		# $cgi->param($key,$value);
	# }	
# }

# #
# # removes plings, comma and semicolon from the cgi->param values
# #
# sub discardHeavy {
	# my($cgi) = shift @_;		
	# foreach my $key ($cgi->param()) {
		# my $value = $cgi->param($key);
		# next if(!defined($value));
		# $value=~ s/'//g;	
		# $value=~ s/;//g;	
		# $value=~ s/,//g;
		# $value=~ s/<//g;
		# $value=~ s/>//g;
		# $value=~ s/"//g;
			
		# $cgi->param($key,$value);
	# }	
# }

# sub discardHeavyString {
	# my $str = shift;		
	# $str=~ s/'//g;	
	# $str=~ s/;//g;
	# $str=~ s/,//g;
	# $str=~ s/<//g;
	# $str=~ s/>//g;
	# $str=~ s/"//g;
	
	# return $str;
# }

sub makeHarmless {
	my($cgi) = shift @_;
	foreach my $key ($cgi->param()) {
		my @list = $cgi->param($key);
		next if @list==0;
		my @newlist;
		foreach my $value (@list) {
			if (defined $value){
				$value=~ s/'/&\#39\;/g; #'		
				$value=~ s/</&lt\;/g;
				$value=~ s/>/&gt\;/g;
				$value=~ s/"/&\#34\;/g; #"
				$value=~ s/%/&\#37\;/g;			
			}
			push @newlist, $value;
		}
		$cgi->param($key, @newlist);
	}		
}



# sub htmlToText {
	# my($value) = shift @_;			
	# $value=~ s/&\#38\;/&/g;				
	# $value=~ s/&\#39\;/'/g;
	# $value=~ s/&\#34\;/"/g; #'
	# $value=~ s/&lt\;/</g;
	# $value=~ s/&gt\;/>/g;		
	# $value=~ s/&\#37\;/%/g;			
	# $value=~ s/&\#40\;/\(/g;
	# $value=~ s/&\#41\;/\)/g;		
	# return $value;		
# }


# sub cookie_expire {
	# my($exp) = shift;
	# my($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(expire_calc("$exp"));
	# my @MoY = ('Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec');
	# my @Day = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
	# $year+=1900;
	# if($mday<10) {	$mday="0$mday";	}
	# $hour--;
	# if($hour<10) {	$hour="0$hour";	}
	# if($min<10) {	$min="0$min"; }
	# if($sec<10) {	$sec="0$sec"; }
	# return ($Day[$wday].", ".$mday." ".$MoY[$mon]." ".$year." ".($hour).":".($min).":".$sec. " GMT");
# }


# sub expire_calc {
    # my($time) = @_;
    # my(%mult) = ('s'=>1,
                 # 'm'=>60,
                 # 'h'=>60*60,
                 # 'd'=>60*60*24,
                 # 'M'=>60*60*24*30,
                 # 'y'=>60*60*24*365);
    # # format for time can be in any of the forms...
    # # "now" -- expire immediately
    # # "+180s" -- in 180 seconds
    # # "+2m" -- in 2 minutes
    # # "+12h" -- in 12 hours
    # # "+1d"  -- in 1 day
    # # "+3M"  -- in 3 months
    # # "+2y"  -- in 2 years
    # # "-3m"  -- 3 minutes ago(!)
    # # If you don't supply one of these forms, we assume you are
    # # specifying the date yourself
    # my($offset);
    # if (!$time || (lc($time) eq 'now')) {
        # $offset = 0;
    # } elsif ($time=~/^([+-]?(?:\d+|\d*\.\d*))([mhdMy]?)/) {
        # $offset = ($mult{$2} || 1)*$1;
    # } else {
        # return $time;
    # }
    # return (time+$offset);
# }

# sub getCookies {
  # my(%cookies);
  # return(%cookies) if(!exists($ENV{'HTTP_COOKIE'}));
  # foreach (split (/; /,$ENV{'HTTP_COOKIE'})){
    # my($key) = split(/=/, $_);
    # my $start = index($_, "=")+1;
    # $cookies{$key} = substr($_, $start);
  # }
  # return(%cookies);
# }

# sub setLocation {
	# my ($frame,$cmd,$js_prefix) = @_;
	# $js_prefix = $js_prefix ? '' : 'javascript:';
	# return $js_prefix . "parent.$frame.location.href='/controller?cmd=$cmd&date='+(new Date).getTime();";
# }

# sub setLocationXSL {
	# my ($frame,$cmd) = @_;
	
	# my @cmd;
	# push(@cmd,'javascript:');
	# push(@cmd,"parent.$frame.location.href='/controller?cmd=$cmd&date='+(new Date).getTime()+");
	# push(@cmd,'id=');
	# push(@cmd,'{$id');
	
	# return "concat('" . join("','",@cmd) . ")";
# }

# sub functions {
	# my ($function,$id) = @_;
	
	# if(defined $id) {
		# if($id !~ /^\d+$/) {
			# $id = "'$id'";
		# }
	# } else {
		# $id = '';
	# }
	
	# return "javascript:parent.functions.$function($id)";
# }

# sub tree_select {
	# my ($node_id) = @_;
	
	# return "javascript:parent.functions.tree_sync('$node_id')";
# }

# sub tree_expand {
	# my ($node_id) = @_;
	
	# return "javascript:parent.browser.tree.toggleItem('$node_id',1)";
# }

# sub tree_expand_select {
	# my ($node_id) = @_;
	
	# return "parent.browser.tree.toggleItem('$node_id',1);parent.functions.tree_sync('$node_id')";
# }

# sub download_file {
	# return qq(window.open('$_[0]','download'););
# }

# sub functionsPopup {
	# my ($function,$id) = @_;
	
	# if(defined $id) {
		# if($id !~ /^\d+$/) {
			# $id = "'$id'";
		# }
	# } else {
		# $id = '';
	# }
	
	# return "javascript:window.opener.parent.functions.$function($id)";
# }

# sub functionsXSL {
	# my ($function,$id_attr) = @_;
	
	# return "concat('javascript:parent.functions.$function('," . '$id' . ",')')";
# }

# sub insertIcon {
	# my ($icon_path) = @_;
	
	# $icon_path .= ".gif" if $icon_path !~ /\.gif$/;	
	# return "string(<img src='/images/$icon_path' width='16' height='16'/>";	
# }

# sub openWindow {
	# my($windowname,$cmd,$w,$h) = @_;
	# #return 'openwindow(\''. $windowname . '\',\'cmd=' . $cmd . "',$w,$h)";
	# return "openwindow('$windowname','cmd=$cmd',$w,$h)";
# }

# sub doSubmit {
	# my ($input_id,$attr_name) = @_;
	# return "javascript:document.data." . $input_id . ".value={$attr_name};document.data.submit();";
# }

# sub closeWindow {
	# return "<html><body onload='window.close()'/></html>";
# }

# sub refreshDirectory {
	# return qq(<html><body onload='window.opener.parent.directory.location.reload();window.close()'/></html>);
# }

# sub refreshTreeNode {
	# my %params = @_;
	
	# my $treenode_id;
	# if(defined $params{id}) {
		# $treenode_id = $params{id};
	# } elsif(defined $params{object}) {
		# $treenode_id = $params{object}->createNodeId();
	# }
	
	# return qq(<html><body onload="window.opener.parent.browser.tree.getNode('$treenode_id').select();window.close()"/></html>);
# }

# sub refreshCurrentTreeNode {
	# return qq(<html><body onload='window.opener.parent.functions.getSelectedNode().select();window.close()'/></html>);
# }

# sub renameTreeNode {
	# my ($node_id,$new_name) = @_;
	# return qq(<html><body onload="window.opener.parent.browser.tree.getNode('$node_id').rename('$new_name');window.close()"/></html>);
# }

# sub setButtonStatus {
	# my ($button_id,$status) = @_;
	# return qq(parent.functions.setButtonStatus('$button_id',$status););
# }


# sub callFunction {
	# my %params = @_;
	
	# my $name = $params{name};
	# my $args = $params{args};
	
	# my $arg_string = join(',', map { $_ =~ /^\d+$/ ? $_ : "'$_'" } @{$args});
	
	# return qq(<html><body onload="window.opener.parent.functions.$name($arg_string);window.close()"/></html>);	
# }

# sub cmd {
	# return "/controller?cmd=" . $_[0];
# }

# sub refreshTable {
	# my %params = @_;
	
	# my $frame 	 = $params{frame};
	# my $table_id = $params{table_id};
	# my $popup	 = $params{popup} ? $params{popup} : 1; 
	
	# my $doc = $popup ? "window.opener" : "";

	# if(defined $frame) {
		# return qq(<html><body onload="$doc.$frame.objGrid_$table_id.loadData();window.close()"/></html>);		
	# } else {
		# return qq(<html><body onload="$doc.objGrid_$table_id.loadData();window.close()"/></html>);
	# }
# }

# sub refresh_directory_frame {
	# my $script = qq(try {window.opener.parent.directory.refresh()} catch (Error) { };);
# }

# sub set_location {
	# my (%params) = @_;
	
	# if(!defined $params{cmd}) {
		# throw Backbone::Exception::ScriptError( message => "cmd not defined" );
	# }
	
	# my $GET_params = "";
	# while (my ($name,$val) = each %params) {
		# next if $name eq 'cmd';
		# $GET_params .= "\&$name=$val";
	# }
	
	# return qq(document.location.href="/controller?cmd=$params{cmd}$GET_params");
# }

# sub javascript {
	# return script_tags(join('',@_));
# }

# sub script_tags {
	# my ($script) = @_;
	# return qq(<html><body><script type="text/javascript">$script</script><body></html>);
# }

1;
